package com.liushuo.view;

import com.liushuo.controller.UserController;
import com.liushuo.util.ThreadLocalUtil;
import org.mindrot.jbcrypt.BCrypt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginRegisterUI extends JFrame{
    private final JTextField idField;
    private final JPasswordField passwordField;

    private final UserController userController = new UserController();

    public LoginRegisterUI() {
        // 换windows风格
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                 | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        Font font = new Font("微软雅黑", Font.PLAIN, 20);

        setTitle("用户登录/注册");
        setBounds(600, 400, 500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel titleLabel = new JLabel("口算练习器");
        titleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 40));
        titleLabel.setBounds(150, 20, 300, 50);
        add(titleLabel);

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(null);
        inputPanel.setBackground(Color.CYAN);
        JLabel label1 = new JLabel("账号：");
        label1.setFont(font);
        label1.setBounds(100, 100, 100, 50);
        idField = new JTextField();
        idField.setFont(font);
        idField.setColumns(10);
        idField.setBounds(200, 100, 200, 50);
        idField.setText("5120227560");
        inputPanel.add(label1);
        inputPanel.add(idField);

        JLabel label2 = new JLabel("密码：");
        label2.setFont(font);
        label2.setBounds(100, 160, 100, 50);
        passwordField = new JPasswordField();
        passwordField.setFont(font);
        passwordField.setColumns(10);
        passwordField.setBounds(200, 160, 200, 50);
        passwordField.setText("123456");
        inputPanel.add(label2);
        inputPanel.add(passwordField);

        JButton loginButton = new JButton("登录");
        loginButton.setFont(font);
        loginButton.setBounds(100, 250, 100, 50);
        loginButton.addActionListener(new LoginButtonListener());
        add(loginButton);

        JButton registerButton = new JButton("注册");
        registerButton.setFont(font);
        registerButton.setBounds(300, 250, 100, 50);
        registerButton.addActionListener(new RegisterButtonListener());
        add(registerButton);

        add(inputPanel);

        setVisible(true);
    }

    private class LoginButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String id = idField.getText();
            String password = new String(passwordField.getPassword());
            String hashedPassword = userController.login(id);
            boolean isLoggedIn = BCrypt.checkpw(password, hashedPassword);
            if (isLoggedIn) {
                ThreadLocalUtil.setUserId(id);
                System.out.println("登录成功, 当前线程用户id：" + ThreadLocalUtil.getUserId());
                dispose();
                new RouterUI();
            } else {
                JOptionPane.showMessageDialog(LoginRegisterUI.this, "登录失败，请检查账号或密码是否正确");
            }
        }
    }

    private class RegisterButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String id = idField.getText();
            String password = new String(passwordField.getPassword());
            String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
            boolean isRegistered = userController.register(id, hashedPassword);
            if (isRegistered) {
                ThreadLocalUtil.setUserId(id);
                System.out.println("注册成功, 当前线程用户id：" + ThreadLocalUtil.getUserId());
                dispose();
                new RouterUI();
            } else {
                JOptionPane.showMessageDialog(LoginRegisterUI.this, "注册失败，请检查账号是否已存在");
            }
        }
    }

//    public static void main(String[] args) {
//        new LoginRegisterUI();
//    }

}
